package com.qidian.controller;


import com.qidian.common.Result;
import com.qidian.entity.Photo;
import com.qidian.service.PhotoService;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import com.qidian.controller.BaseController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author qidian
 * @since 2020-11-26
 */
@RestController
public class PhotoController extends BaseController {

    @Autowired
    PhotoService photoService;

    @GetMapping("/photo/{id}")
    public Result getPhoto(@PathVariable("id") Integer id) {
        Photo photo = photoService.getById(id);
        return Result.success(photo);
    }

    @PostMapping("/photo")
    public Result addPhoto(Photo photo, MultipartFile[] uploadFiles, HttpServletRequest request, Model model) {
        //1，对文件数组做判空操作
        for (int i=0;i<uploadFiles.length;i++){
            if (uploadFiles[i].isEmpty()) {
                return Result.fail("上传的文件不能为空");
            }
        }

        //2.定义文件的存储路径
        String realPath = request.getSession().getServletContext().getRealPath("/uploadFile/");
        File dir = new File(realPath);
        if (!dir.isDirectory()) {//文件目录不存在，就创建一个
            dir.mkdirs();
        }
        Map<Object, Object> data = new HashMap<>();
        data.put("photo", photo);
        try {

            String filePathS = "";
            //3，遍历文件数组，一个个上传
            for (int i=0;i<uploadFiles.length;i++){
                String sub = uploadFiles[i].getOriginalFilename().substring(uploadFiles[i].getOriginalFilename().lastIndexOf("."));
                if(sub == null){
                    return Result.fail("上传的文件格式不正确");
                }
                String filename = UUID.randomUUID().toString()+sub;
                //服务端保存的文件对象
                File fileServer = new File(dir, filename);
                System.out.println("file文件真实路径:" + fileServer.getAbsolutePath());
                //2，实现上传
                uploadFiles[i].transferTo(fileServer);
                String img = request.getScheme() + "://" +
                        request.getServerName() + ":"
                        + request.getServerPort() +
                        request.getContextPath()
                        + "/uploadFile/" + filename;
                //4，返回可供访问的网络路径
                data.put("img", img);//返回最后一个元素的地址作为测试
                photo.setAddress(img);
                photo.setTime(new Date());
                photoService.save(photo);
            }
            return Result.success(data);
        } catch (Exception e) {
            e.printStackTrace();
        }
        //photoService.save(photo);

        return Result.success(data);
    }
}
